package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password) {

        User user = userService.login(username, password);
        ModelAndView mv = new ModelAndView();

        if (user == null) {
            mv.setViewName("index");
            mv.addObject("message","用户名或者密码错误");
        } else {
            mv.setViewName("student");
        }

        return mv;
    }

    @RequestMapping("/save")
    public ModelAndView save(User user) {

        ModelAndView mv = new ModelAndView();
        userService.add(user);
        mv.setViewName("index");

        return mv;
    }

}
